﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class NamKeHoachDAO :SqlDAO
    {
        public NamKeHoachDAO(string connectionString, System.Type type = null) :
            base(connectionString, type) { ;}

        //public const string PROCEDURE_GET_LIST_PAGING = "spChiTieu_GetListPaging";
        public const string PROCEDURE_INSERT_UPDATE = "pNamKeHoach_InsertUpdate";
        public const string PROCEDURE_DELETE = "pNamKeHoach_DELETE";
        public const string PROCEDURE_GET_BY_ID = "pNamKeHoach_GetByID";
        public const string PROCEDURE_GET_ALL = "pNamKeHoach_GetGetAll";
        public const string PROCEDURE_UPDATE = "spNamKeHoach_Update";
        
        public const string PROCEDURE_GET_STATUS = "spStatus_GetAll";
        public const string PROCEDURE_CHECK = "spNamKeHoach_Check";
        public const string PROCEDURE_CHECK_PHAN_HOI = "spNamKeHoach_CheckPhanHoi";
        public const string PROCEDURE_GET_BY_NAM = "pNamKeHoach_GetByNam";
        public const string PROCEDURE_CHECK_CHOT = "spKehoach_Checkchot";
        public const string PARAM_NAM_KE_HOACH_ID = "@NAM_KE_HOACH_ID";
        public const string PARAM_NAM_KE_HOACH = "@NAM_KE_HOACH";
        public const string PARAM_STATUS_ID = "@STATUS_ID";
        public const string PARAM_STATUS_DATE = "@STATUS_DATE";
        public const string PARAM_NHOM_CHI_TIEU = "@NHOM_CHI_TIEU";
        public const string PROCEDURE_NAM_CHECK_CHOT = "spNamKeHoach_CheckChot";
        

        public int Save(NamKeHoach model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_UPDATE);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, model.NAM_KE_HOACH_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH, model.NAM_KE_HOACH));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, model.STATUS_ID));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_DATE, model.STATUS_DATE));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public int Check(int namkhid, int statusid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID,namkhid));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, statusid));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public List<NAMDV> CheckPhaiHoiChot(int namkhid, int statusid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK_PHAN_HOI);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, statusid));
                return GetList<NAMDV>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public int CheckChot(int namkhid, int statusid, int nhomchitieu)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_CHECK_CHOT);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, statusid));
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_CHI_TIEU, nhomchitieu));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public int NamkehoachUpdate(int namkhid, int statusid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_UPDATE);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, statusid));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public int CheckNamChot(int namkhid, int statusid, int nhomchitieu)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_NAM_CHECK_CHOT);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(CreateParameter(PARAM_STATUS_ID, statusid));
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_CHI_TIEU, nhomchitieu));
                return Convert.ToInt32(ExecuteScalar(cmd));
            }
            catch
            {
                throw;
            }
        }
        public bool Delete(int id)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, id));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
        public List<Status> Getallstatus()
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_STATUS);
                return GetList<Status>(cmd);
            }
            catch
            {
                throw;
            }
        }

        public NamKeHoach GetById(int id)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH_ID, id));

                return GetSingle<NamKeHoach>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public NamKeHoach GetByNam(string namkh)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_NAM);
                cmd.Parameters.Add(CreateParameter(PARAM_NAM_KE_HOACH, namkh));
                return GetSingle<NamKeHoach>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List< NamKeHoach> GetAllNamkehoach()
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_ALL);
                return GetList<NamKeHoach>(cmd);
            }
            catch
            {
                throw;
            }
        }
    }
}
